var $search=$("#search_button");

function serDlg() {
    $("#searchDlg").modal();
    document.getElementById("search_input").value="";
}

function validateSearchId() {
    return $("#select").validate({
        rules: {
            search_input: {
                digits: true
            }
        },
        messages: {
            search_input: {
                digits: "id必须是整数！"
            }
        }
    });
}

$search.click(function () {
    var value = $("#search_select ").val();//获取select的值
    if(value==="objectId") {
        if(validateSearchId().form()) {
            searchId();
        }
    } else if(value==="objectName"){
        searchName();
    } else if(value==="abilities"){
        searchAbilities();
    } else if(value==="objRelationship"){
        searchRelationship();
    } else if(value==="chara"){
        searchChara();
    }
});

function searchId() {
    var id = $("#search_input").val().replace(/(^\s*)|(\s*$)/g, "");//replace用于去掉首尾的空格
    if(id!==null&&id!==undefined&&id!=="") {
        $('#searchDlg').modal('hide').on("hidden", function () {
            $(this).removeData("modal");
        });
        $table.jqGrid('clearGridData');
        $table.jqGrid('setGridParam', {  // 重新加载数据
            url: "/object/find/id",
            datatype: 'json',
            mtype: "post",
            postData: {"id": id}
        }).trigger("reloadGrid");
    }
    else {
        $("#searchDlg").modal('hide').on("hidden", function () {
            $(this).removeData("modal");
        });
    }
}

function searchName() {
    var id = $("#search_input").val().replace(/(^\s*)|(\s*$)/g, "");//replace用于去掉首尾的空格
    if(id!==null&&id!==undefined&&id!=="") {
        $('#searchDlg').modal('hide').on("hidden", function () {
            $(this).removeData("modal");
        });
        $table.jqGrid('clearGridData');
        $table.jqGrid('setGridParam', {  // 重新加载数据
            url: "/object/find/name",
            datatype: 'json',
            mtype: "post",
            postData: {"name": id}
        }).trigger("reloadGrid");
    }
    else {
        $("#searchDlg").modal('hide').on("hidden", function () {
            $(this).removeData("modal");
        });
    }
}

function searchAbilities() {
    var id = $("#search_input").val().replace(/(^\s*)|(\s*$)/g, "");//replace用于去掉首尾的空格
    if(id!==null&&id!==undefined&&id!=="") {
        $('#searchDlg').modal('hide').on("hidden", function () {
            $(this).removeData("modal");
        });
        $table.jqGrid('clearGridData');
        $table.jqGrid('setGridParam', {  // 重新加载数据
            url: "/object/find/abilities",
            datatype: 'json',
            mtype: "post",
            postData: {"abilities": id}
        }).trigger("reloadGrid");
    }
    else {
        $("#searchDlg").modal('hide').on("hidden", function () {
            $(this).removeData("modal");
        });
    }
}

function searchRelationship() {
    var id = $("#search_input").val().replace(/(^\s*)|(\s*$)/g, "");//replace用于去掉首尾的空格
    if(id!==null&&id!==undefined&&id!=="") {
        $('#searchDlg').modal('hide').on("hidden", function () {
            $(this).removeData("modal");
        });
        $table.jqGrid('clearGridData');
        $table.jqGrid('setGridParam', {  // 重新加载数据
            url: "/object/find/relationship",
            datatype: 'json',
            mtype: "post",
            postData: {"relationship": id}
        }).trigger("reloadGrid");
    }
    else {
        $("#searchDlg").modal('hide').on("hidden", function () {
            $(this).removeData("modal");
        });
    }
}

function searchChara() {
    var id = $("#search_input").val().replace(/(^\s*)|(\s*$)/g, "");//replace用于去掉首尾的空格
    if(id!==null&&id!==undefined&&id!=="") {
        $('#searchDlg').modal('hide').on("hidden", function () {
            $(this).removeData("modal");
        });
        $table.jqGrid('clearGridData');
        $table.jqGrid('setGridParam', {  // 重新加载数据
            url: "/object/find/chara",
            datatype: 'json',
            mtype: "post",
            postData: {"chara": id}
        }).trigger("reloadGrid");
    }
    else {
        $("#searchDlg").modal('hide').on("hidden", function () {
            $(this).removeData("modal");
        });
    }
}